package com.seleniti.enpareja.android;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class WebViewActivity extends Activity /* implements HasPlaceData */{
	/**
	 * Query key
	 */
	public static final String URL_QUERY_KEY = "query";
	// private static ClientFactoryProvider<? extends PlaceController>
	// placeControllerProvider;
	private WebView webView;

	@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		ActionBar actionBar = getActionBar();
		if (actionBar != null) {
			actionBar.hide();
		}

		setContentView(R.layout.activity_web_view);

		this.webView = (WebView) findViewById(R.id.webview);

		WebSettings webSettings = webView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

		webView.addJavascriptInterface(new WebAppInterface(this), "Android");
		webView.setWebViewClient(new WebViewClient());
		webView.setWebChromeClient(new WebChromeClient());

		String url = "https://en-pareja.appspot.com";

		Intent in = getIntent();

		String urlQuery = in.getStringExtra(URL_QUERY_KEY);
		if (urlQuery != null) {
			url += "/#place:" + urlQuery;
		}

		webView.loadUrl(url);

		CookieManager.getInstance().setAcceptCookie(true);
	}

	@Override
	public void onBackPressed() {
		String placeId = getPlaceId();
		if (webView.canGoBack() && !"Main".equals(placeId) && !"LoginPlace".equals(placeId)) {
			webView.goBack();
		} else {
			super.onBackPressed();
			//
			// PlaceController placeController =
			// placeControllerProvider.forThisObject(this);
			//
			// if (placeController != null) {
			// synchronized (placeController) {
			// placeController.notifyPlaceChanged();
			// }
			// }
		}
	}

	// public static ClientFactoryProvider<? extends PlaceController>
	// getPlaceControllerProvider() {
	// return placeControllerProvider;
	// }
	//
	// public static void setPlaceControllerProvider(
	// ClientFactoryProvider<? extends PlaceController> placeControllerProvider)
	// {
	// WebViewActivity.placeControllerProvider = placeControllerProvider;
	// }

	// @Override
	// public Enum<? extends IsPlace> getPlaceId() {
	// String url = webView.getUrl();
	//
	// int idx = url.indexOf("/#place:");
	// if (idx >= 0) {
	// idx += "/#place:".length();
	//
	// try {
	// return EnParejaPlace.valueOf(url.substring(idx));
	// } catch (Exception e) {
	// return null;
	// }
	//
	// } else {
	//
	// return null;
	// }
	// }

	// @Override
	public String getPlaceId() {
		String url = webView.getUrl();

		int idx = url.indexOf("/#place:");
		if (idx >= 0) {
			idx += "/#place:".length();

			try {
				return url.substring(idx);
			} catch (Exception e) {
				return null;
			}

		} else {

			return null;
		}
	}
}
